package com.huishen.edrivenew.bean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.huishen.edrivenew.base.BaseBean;
import com.huishen.edrivenew.util.StringUtils;
import com.huishen.edrivenew.util.TLog;

public class DateBean extends BaseBean {
    
    public String sDate; // 1970-1-1;
    public String sDateZh; // 1970-1-1;
    public Date date;
    
    public int day = -1;
    public boolean isToday = false;

    // km 2 3
    public int type;


    /**
     * 是否可点击
     * 7天之外的都不可点击；
     */
    public boolean canClick = false;
    
    /** -1 不可点击； 0 可点击； 1 已预约；2 已约满;  **/
    public int state = -1;
    
    
    public DateBean(Date date) {
        this.date = date;
    }
    
    public DateBean() {
        this.date = new Date();
        isToday = true;
    }
    
    public DateBean(int addDay) {
        Calendar calendar = Calendar.getInstance();  
        calendar.add(Calendar.DAY_OF_YEAR, addDay);  
        this.date = calendar.getTime();
    }
    
    
    public void setState() {
        
    }
    public DateBean setCanClick(Boolean can) {
        this.canClick = can;
        return this;
    }
    
    
    public int getDayNumber() {
        if(day != -1) {
            return day;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));  
        cal.setTime(date);
        
        day = cal.get(Calendar.DAY_OF_MONTH);
        return day;
    }
    
    public String getDateString() {
        if(StringUtils.isEmpty(sDate)) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
            sDate = df.format(date);
        }
        return sDate;
    }

    public String getDateStringZh() {
        if(StringUtils.isEmpty(sDateZh)) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy年M月d日");// 设置日期格式
            sDateZh = df.format(date);
        }
        return sDateZh;
    }
    
    
    /**
     * 获取今天星期几 
     */
    public static int getTodayWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));  
        int dayweek = cal.get(Calendar.DAY_OF_WEEK);
        return dayweek;
    }
    
    
    /**
     * 获取14天日历数据
     * @return
     */
    public static DateBean[] getDays() {
        DateBean[] days = new DateBean[14];
        
        // 设置是否可点击
        int clickcount = -1;
        
        // 今天在列表中的索引
        int todayIndex = DateBean.getTodayWeek() - 1;
        for(int i=0;i<14;i++) {
            if(clickcount != -1) {
                clickcount++;
            }
            
            if(i == todayIndex) {
                days[i] = new DateBean().setCanClick(true);
                clickcount = 0;
            } else {
                if(clickcount > 7) {
                    clickcount = -1;
                }
                
                days[i] = new DateBean(i - todayIndex).setCanClick(clickcount != -1);
            }
            

        }
        return days;
    }
    
    
    public static void setState(DateBean[] days, ListLessonInfoBean beanList) {
        for(DateBean day : days) {
            if(!day.canClick) {
                continue;
            }
            day.state = 0;
            
            for(LessInfoBean bean : beanList.listLessinfo) {
                if(! bean.isTheDay(day.getDateString())) {
                    continue;
                }
                
                if(bean.countlessinfo >= bean.kmcount) {
                    // 不能预约 已满
                    day.state = 2;
                }
                break;
            }
            for(LessInfoBean bean : beanList.stuLessonInfoStatus) {
                if(! bean.isTheDay(day.getDateString())) {
                    continue;
                }
                // 已有约
                day.state = 1;
                break;
            }
            
        }
        for(DateBean da:days) {
            TLog.log("in  " + da.state);
        }
    }
    
}
